package tree.test129;

import listbyorder.utils.TreeNode;

/**
 * @author code_yc
 * @version 1.0
 * @date 2020/7/17 9:57
 */
public class Solution1 {
    public int sumNumbers(TreeNode root) {
        if (root == null) {
            return 0;
        }
        return getAns(root, 0);
    }

    private int getAns(TreeNode root, int sum) {
        int curSum = sum * 10 + root.val;

        // 如果达到了叶子节点
        if (root.left == null && root.right == null) {
            return curSum;
        }

        int ans = 0;
        if (root.left != null) {
            ans += getAns(root.left, curSum);
        }
        if (root.right != null) {
            ans += getAns(root.right, curSum);
        }
        return ans;
    }

}
